package org.jeecg.modules.supplier.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.annotations.Param;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.modules.supplier.entity.PdSupplier;
import org.jeecg.modules.supplier.service.PdSupplierService;
import org.jeecg.modules.supplier.vo.ProductVo;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.vo.RegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 供应商表 前端控制器
 * </p>
 *
 * @author twm
 * @since 2021-11-03
 */
@RestController
@RequestMapping("/pdSupplier")
public class PdSupplierController {


    @Autowired
    public PdSupplierService supplierService;

    @GetMapping("checkSupplier")
    public Result checkSupplier(PdSupplier supplier) {

        Result<Boolean> result = new Result<>();
        //如果此参数为false则程序发生异常
        result.setResult(true);
        try {
            //通过传入信息查询新的用户信息
            PdSupplier user = supplierService.getOne(new QueryWrapper<PdSupplier>(supplier));
            if (user != null) {
                result.setSuccess(false);
                result.setMessage("用户账号已存在");
                return result;
            }

        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage(e.getMessage());
            return result;
        }
        result.setSuccess(true);
        return result;
    }


    @DeleteMapping("delect" )
    public Result delete(@Param("id") String id) {
        LambdaQueryWrapper eq = new LambdaQueryWrapper<PdSupplier>().eq(PdSupplier::getId,id);
        boolean remove = supplierService.remove(eq);
        return Result.ok(remove);
    }

    /**
     *
     *   paseSize : this.paseSize,
     *         currentPage : this.currentPage
     * @return
     */
    @GetMapping("find_supplier_info")
    public Result findSupplierInfo(@RequestParam(value = "paseSize") Integer paseSize,
                                   @RequestParam("currentPage") Integer currentPage,
                                   @RequestParam(value = "supplierName",required = false) String supplierName) {

        Page<PdSupplier> page = new Page<>(currentPage,paseSize);
        Page<PdSupplier> list  = supplierService.findSupplierInfo(page,supplierName);
//        List<PdSupplier> list = supplierService.lambdaQuery().list();

        Map<String,Object> returnValue = new HashMap<>(2);

        List<RegisterVo> collect = list.getRecords().stream()
                .map(item -> {
                    RegisterVo vo = new RegisterVo();
                    vo.setId(item.getId());
                    vo.setCode(item.getSocialCreditCode());
                    vo.setCompanyName(item.getName());
                    vo.setUserName(item.getUserName());
                    String decrypt = PasswordUtil.decrypt(item.getPassword(), item.getUserName(), item.getSalt());
                    vo.setPassword(decrypt);
                    vo.setCreatTime(item.getCreateTime());
                    return vo;
                }).collect(Collectors.toList());

        returnValue.put("total",list.getTotal());
        returnValue.put("value",collect);


        return Result.ok(returnValue);
    }

}

